<template>
  <div>
    <question
      :questionId="questionId"
      :dialogFormVisible="dialogFormVisible"
      @update:dialogFormVisible="handleUpdate"
      v-if="dialogFormVisible"
      :disabled="editFlag"
    ></question>
    <el-dialog :visible.sync="show" title="上 传"
      ><shangChuan :url="url" @shangChuanEvent="shangChuanEvent"></shangChuan
    ></el-dialog>
    <el-form :model="form" inline>
      <el-form-item label="题目名称"
        ><el-input v-model="form.name" placeholder="请输入题目名称"></el-input
      ></el-form-item>
      <el-button type="primary" @click="search">查 询</el-button>
      <el-button type="primary" @click="add">添 加</el-button>
      <el-button type="primary" @click="upload">上 传</el-button>
    </el-form>
    <el-table :data="tableData">
      <el-table-column label="模块" prop="subject"></el-table-column>
      <el-table-column label="章节" prop="chapter"></el-table-column>
      <el-table-column label="子章节" prop="subChapter"></el-table-column>
      <el-table-column label="问题" prop="question"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="detailHandler(scope.row)" size="mini" type="text"
            >查看</el-button
          >
          <el-button @click="detail(scope.row)" size="mini" type="text"
            >编辑</el-button
          >
          <el-button @click="deleteHandler(scope.row)" size="mini" type="text"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :current-page="currentPage"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具 js，第三方插件 js，json 文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import questionApi from "@/api/question/index";
import question from "@/views/question/question";
import shangChuan from "./shangChuan.vue";
import host from "@/utils/host";
export default {
  //import 引入的组件需要注入到对象中才能使用
  components: { question, shangChuan },
  props: {},
  data() {
    //这里存放数据
    return {
      editFlag: false,
      url: "",
      show: false,
      form: { name: "" },
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      questionId: 0,
      dialogFormVisible: false,
    };
  },
  //计算属性 类似于 data 概念
  computed: {},
  //监控 data 中的数据变化
  watch: {},
  //方法集合
  methods: {
    shangChuanEvent(payload) {
      if (payload == "success") {
        this.show = false;
        this.list(this.currentPage);
        this.getTotal();
      }
    },
    upload() {
      this.show = true;
      this.url = "http://" + host + ":8080/elearning/question/upload";
    },

    deleteHandler(data) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          questionApi.delete(data).then((res) => {
            this.$message({
              type: "success",
              message: `${res.message}`,
            });
            this.list(this.currentPage);
            this.getTotal();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    add() {
      this.dialogFormVisible = true;
      this.questionId = 0;
    },
    search() {
      this.getTotal();
      this.list(1);
      this.currentPage = 1;
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.list(currentPage);
    },
    list: async function (currentPage) {
      const res = await questionApi.list(
        this.form.name,
        currentPage,
        this.pageSize
      );
      this.tableData = res.data;
    },
    getTotal: async function () {
      const res = await questionApi.getTotal(this.form.name);
      this.total = res.data;
    },
    detail(data) {
      this.editFlag = false;
      this.questionId = data.id;
      this.dialogFormVisible = true;
    },
    detailHandler(data) {
      this.editFlag = true;
      this.questionId = data.id;
      this.dialogFormVisible = true;
    },
    handleUpdate(value) {
      if (value.type === "detail") {
        this.dialogFormVisible = value.val;
      }
    },
  },
  //生命周期 - 创建完成（可以访问当前 this 实例）
  created() {
    this.list(1);
    this.getTotal();
  },
  //生命周期 - 挂载完成（可以访问 DOM 元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有 keep-alive 缓存功能，这个函数会触发
};
</script>
<style scoped></style>
